package com.zadtek.shopmall.activities;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.InfoBean;
import com.zadtek.shopmall.bean.OperateResponseBean2;
import com.zadtek.shopmall.bean.OperateResponseBean3;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MyInfoListActivity extends AbsListActivity {
    private MyInfoAdapter adapter;
    private List<InfoBean> data = new LinkedList<>();
    private int page = 1;
    private int pagesize =10;

    @Override
    protected String getTopTitle() {
        return "我发布的信息";
    }

    @Override
    public BaseQuickAdapter getAdapter() {

        adapter = new MyInfoAdapter(R.layout.my_info_list_item,data);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_hide:
                        changeInfoStatus(0,position);
                        break;
                    case R.id.tv_show:
                        changeInfoStatus(1,position);
                        break;
                }
            }
        });
        return adapter;
    }

    private void changeInfoStatus(final int i, final int position) {
        Map<String, Object> param = NetTool.newParams();
        param.put("status",i);
        param.put("id",data.get(position).getId());
        NetTool.getApi().changeInfoStatus(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>(this) {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean3> value) {
                        if(value.getCode()==0 && value.getData().isSuccess()){
                            ToastUtil.showToast(value.getData().getMessage());
                            data.get(position).setStatus(i);
                            adapter.notifyItemChanged(position);
                        }else{
                            ToastUtil.showToast(value.getData().getMessage());
                        }
                    }
                });
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getInfo(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<InfoBean>>>() {
                    @Override
                    public void onData(BaseResponse<List<InfoBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                adapter.notifyDataSetChanged();
                                adapter.loadMoreComplete();
                                page++;
                            }else{
                                adapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    public void OnItemClick(int position) {

    }

    private class MyInfoAdapter extends BaseQuickAdapter<InfoBean,BaseViewHolder>{

        public MyInfoAdapter(int layoutResId, @Nullable List<InfoBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, InfoBean item) {
            helper.addOnClickListener(R.id.tv_show);
            helper.addOnClickListener(R.id.tv_hide);
            helper.setText(R.id.tv_title,item.getName());
            helper.setText(R.id.tv_time,"发布于"+item.getAddtime());
            helper.setText(R.id.tv_seecount,"浏览"+item.getSee_num()+"次");
            Glide.with(mActivity).load(item.getCover()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_info_img));
            if(item.getStatus() == 1){
                helper.setText(R.id.tv_status,"显示中");
                helper.getView(R.id.tv_show).setVisibility(View.GONE);
                helper.getView(R.id.tv_hide).setVisibility(View.VISIBLE);
            }else if(item.getStatus() == 0){
                helper.setText(R.id.tv_status,"不显示");
                helper.getView(R.id.tv_show).setVisibility(View.VISIBLE);
                helper.getView(R.id.tv_hide).setVisibility(View.GONE);
            }
        }
    }
}
